<?php

namespace app\admin\controller;

use app\admin\logic\WorkStationLogic;

/**
 * 工位api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/09/19
 * Time: 09:50
 */
class WorkStation extends Base
{
    public function __construct()
    {
        parent::__construct();
        $this->ws_logic = new WorkStationLogic();
    }

    /**
     * 获取工位
     * @return \think\response\Json
     */
    public function ajax_get_ws()
    {
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        //获取工位
        $r = $this->ws_logic->get_ws($params);

        $data['list'] = $r['data']['list'];
        $data['total_num'] = $r['data']['total_num'];
        return json($this->ws_logic->get_response(1, 'OK', $data));
    }

    /**
     * 获取工位详情
     * @return \think\response\Json
     */
    public function ajax_get_ws_detail()
    {
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        $maps = [];
        $maps[] = ['id', '=', $params['wsid']];
        $ws_detail = model('work_station')->get_detail($maps);

        $ws_detail['work_type_zw'] = $ws_detail->work_type_name;
        $ws_detail['price'] = deal_number_div_100($ws_detail['price']);
        $ws_detail['is_indoor_air_zw'] = $ws_detail->is_indoor_air_name;
        $ws_detail['ws_pic'] = !empty($ws_detail['ws_pic']) ? json_decode($ws_detail['ws_pic'], true) : [];
        $ws_detail['company_pic'] = !empty($ws_detail['company_pic']) ? json_decode($ws_detail['company_pic'], true) : [];
        $ws_detail['company_work_preson'] = !empty($ws_detail['company_work_preson']) ? json_decode($ws_detail['company_work_preson'], true) : [];

        //查找区域
        $r_maps = [];
        $r_maps[] = ['id', '=', $ws_detail['regionid']];
        $r_detail = model('region')->get_detail($r_maps, 'id,floorid,name');
        $ws_detail['region_zw'] = $r_detail['name'];

        //查找楼层
        $f_maps = [];
        $f_maps[] = ['id', '=', $r_detail['floorid']];
        $f_detail = model('floor')->get_detail($f_maps, 'id,branchid,name');
        $ws_detail['floor_zw'] = $f_detail['name'];

        //查找分部
        $b_maps = [];
        $b_maps[] = ['id', '=', $f_detail['branchid']];
        $b_detail = model('branch')->get_detail($b_maps, 'id,name');
        $ws_detail['branch_zw'] = $b_detail['name'];

        $data['ws_detail'] = $ws_detail;
        return json($this->ws_logic->get_response(1, 'OK', $data));
    }

    /**
     * 获取历次沟通备注
     * @return \think\response\Json
     */
    public function ajax_get_remark()
    {
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        $maps = $this->ws_logic->get_search_remark_maps($params['wsid']);
        $limit = ($params['page'] - 1) * $params['rows'];

        $list = model('cp_region_ws')->get_list($maps, 'id,openid,remark,c_time', 'id desc', '', $limit, $params['rows']);
        foreach ($list as $k => $v) {
            //查找用户
            $u_maps = [];
            $u_maps[] = ['openid', '=', $v['openid']];
            $u_detail = model('user')->get_detail($u_maps, 'id,username');
            $list[$k]['username'] = $u_detail['username'];
        }

        $total_num = model('cp_region_ws')->get_count($maps, 'id');

        $data['list'] = $list;
        $data['total_num'] = $total_num;
        return json($this->ws_logic->get_response(1, 'OK', $data));
    }

    /**
     * 新增工位
     * @return \think\response\Json
     */
    public function ajax_add_ws()
    {
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        //检查新增工位规则
        $r = $this->ws_logic->check_add_ws_rule($params);
        if (!$r['code']) {
            return json($r);
        }

        $ws_data = [];
        $ws_data['regionid'] = $params['regionid'];
        $ws_data['number'] = trim($params['number']);
        $ws_data['work_type'] = $params['work_type'];
        $ws_data['measure'] = trim($params['measure']);
        $ws_data['price'] = deal_number_mul_100(trim($params['price']));
        $ws_data['suggest_person_num'] = trim($params['suggest_person_num']);
        $ws_data['window_direction'] = trim($params['window_direction']);
        $ws_data['is_indoor_air'] = $params['is_indoor_air'];
        $ws_data['ws_pic'] = json_encode($params['ws_pic'], JSON_UNESCAPED_UNICODE);
        $ws_data['company_pic'] = json_encode($params['company_pic'], JSON_UNESCAPED_UNICODE);
        $ws_data['company_name'] = trim($params['company_name']);
        $ws_data['company_person'] = trim($params['company_person']);
        $ws_data['company_tel'] = trim($params['company_tel']);
        $ws_data['company_work_preson'] = json_encode($params['company_work_preson'], JSON_UNESCAPED_UNICODE);
        $ws_data['company_out_time'] = !empty(trim($params['company_out_time'])) ? trim($params['company_out_time']) : '2019-11-01';
        $ws_data['company_in_time'] = !empty(trim($params['company_in_time'])) ? trim($params['company_in_time']) : '2019-11-01';
        $ws_data['company_introduce'] = trim($params['company_introduce']);
        $ws_data['company_business'] = trim($params['company_business']);
        $ws_data['is_show_company'] = $params['is_show_company'];
        $r = model('work_station')->insert($ws_data);
        if ($r) {
            return json($this->ws_logic->get_response(1, '新增成功'));
        }
        return json($this->ws_logic->get_response(0, '新增失败'));
    }

    /**
     * 修改工位
     * @return \think\response\Json
     */
    public function ajax_save_ws()
    {
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        //检查修改工位规则
        $r = $this->ws_logic->check_save_ws_rule($params);
        if (!$r['code']) {
            return json($r);
        }
        $ws_detail = $r['data']['ws_detail'];

        $ws_data = [];
        $ws_data['number'] = trim($params['number']);
        $ws_data['work_type'] = $params['work_type'];
        $ws_data['measure'] = trim($params['measure']);
        $ws_data['price'] = deal_number_mul_100(trim($params['price']));
        $ws_data['suggest_person_num'] = trim($params['suggest_person_num']);
        $ws_data['window_direction'] = trim($params['window_direction']);
        $ws_data['is_indoor_air'] = $params['is_indoor_air'];
        $ws_data['ws_pic'] = json_encode($params['ws_pic'], JSON_UNESCAPED_UNICODE);

        $maps = [];
        $maps[] = ['id', '=', $ws_detail['id']];
        $r = model('work_station')->force()->save($ws_data, $maps);
        if ($r !== false) {
            return json($this->ws_logic->get_response(1, '修改成功'));
        }
        return json($this->ws_logic->get_response(0, '修改失败'));
    }

    /**
     * 修改公司
     * @return \think\response\Json
     */
    public function ajax_save_company()
    {
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        //检查修改公司规则
        $r = $this->ws_logic->check_save_company_rule($params);
        if (!$r['code']) {
            return json($r);
        }
        $ws_detail = $r['data']['ws_detail'];

        $ws_data = [];
        $ws_data['company_pic'] = json_encode($params['company_pic'], JSON_UNESCAPED_UNICODE);
        $ws_data['company_name'] = trim($params['company_name']);
        $ws_data['company_person'] = trim($params['company_person']);
        $ws_data['company_tel'] = trim($params['company_tel']);
        $ws_data['company_work_preson'] = json_encode($params['company_work_preson'], JSON_UNESCAPED_UNICODE);
        $ws_data['company_out_time'] = trim($params['company_out_time']);
        $ws_data['company_in_time'] = trim($params['company_in_time']);
        $ws_data['company_introduce'] = trim($params['company_introduce']);
        $ws_data['company_business'] = trim($params['company_business']);
        $ws_data['is_show_company'] = $params['is_show_company'];

        $maps = [];
        $maps[] = ['id', '=', $ws_detail['id']];
        $r = model('work_station')->force()->save($ws_data, $maps);
        if ($r !== false) {
            return json($this->ws_logic->get_response(1, '修改成功'));
        }
        return json($this->ws_logic->get_response(0, '修改失败'));
    }

    /**
     * 删除工位
     * @return \think\response\Json
     */
    public function ajax_del_ws()
    {
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        $r = model('work_station')->destroy($params['wsid']);
        if ($r) {
            return json($this->ws_logic->get_response(1, '删除成功'));
        }
        return json($this->ws_logic->get_response(0, '删除失败'));
    }

    /**
     * 导入工位
     * @return \think\response\Json
     */
    public function import_ws()
    {
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        $file = request()->file('ws_file');
        $r = $this->ws_logic->import_ws($file);
        if (!$r['code']) {
            return json($r);
        }

        $total_num = $r['data']['total_num'];
        $success_num = $r['data']['success_num'];
        return json($this->ws_logic->get_response(1, '导入成功，总数量：' . $total_num . '，成功数量：' . $success_num));
    }

    /**
     * 生成工位小程序码
     * @return \think\response\Json
     */
    public function ajax_gen_ws_mini_code()
    {
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        $wm = new \app\common\controller\WxMini();

        $maps = [];
        $list = model('work_station')->get_list($maps, 'id', 'id desc');
        foreach ($list as $k => $v) {
            $scene = 'spaceid=' . $v['id'];

            //生成小程序码
            $r = $wm->gen_mini_code($scene);
            if (!$r) {
                return json($this->ws_logic->get_response(0, '生成失败'));
            }

            $ws_maps = [];
            $ws_maps[] = ['id', '=', $v['id']];

            $ws_data = [];
            $ws_data['wechat_mini_code_pic'] = $r;
            $r = model('work_station')->force()->save($ws_data, $ws_maps);
        }

        return json($this->ws_logic->get_response(1, '生成成功，总数量：' . count($list)));
    }

    /**
     * 批量下载小程序码
     * @return \think\response\Json
     */
    public function ajax_down_ws_mini_code()
    {
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        //获取工位
        $r = $this->ws_logic->get_ws($params);
        $list = $r['data']['list'];

        //生成工位/办公室pdf
        $p = new \app\common\controller\Pdf();
        $r = $p->gen_ws_pdf($list);

        return json($this->ws_logic->get_response(1, 'OK'));
    }
}
